﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using InfoJetSoft.Service.Entity;

//CID 0678 //2010.11.03 //Begin
namespace InfoJetSoft.Service.Util
{
    public class XCTransform
    {
        private XslTransform xTransform = null;
        private XslCompiledTransform cTransform = null;

        public XCTransform(Manifest manifest, XmlDocument xslDoc)
        {
            if (ConfigUtil.EnableXSLTC())
            {
                this.cTransform = new XslCompiledTransform(true);
                this.cTransform.Load(xslDoc.CreateNavigator());
            }
            else
            {
                this.xTransform = new XslTransform();
                //CID 0678 //2010.11.03 //Begin
                this.xTransform.Load(xslDoc.CreateNavigator());
                //CID 0678 //2010.11.03 //End
            }
        }

        public XmlDocument Transform(XmlElement root, XsltArgumentList argList)
        {
            XPathNavigator nav = root.CreateNavigator();

            MemoryStream outputStream = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(outputStream, System.Text.UTF8Encoding.UTF8);

            if (this.cTransform != null)
            {
                this.cTransform.Transform(nav, argList, writer);
            }
            else
            {
                //CID 0266 //2009.03.31 //Begin
                this.xTransform.Transform(nav, argList, writer);
                //CID 0266 //2009.03.31 //End
            }

            outputStream.Seek(0, System.IO.SeekOrigin.Begin);

            XmlDocument htmlDoc = new XmlDocument();
            htmlDoc.Load(outputStream);
            outputStream.Close();

            return htmlDoc;
        }
    }
}
//CID 0678 //2010.11.03 //End